﻿using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;

namespace CYTS.Aoyou.Framework.Database.LinqToSql
{
    public class XmlMappingSourceFactory
    {
        private static Dictionary<Type, XmlMappingSource> dicTypeMappingSourceCollection = new Dictionary<Type, XmlMappingSource>();
        private static object lockObj = new object();

        static XmlMappingSourceFactory()
        {
            
        }

        public static XmlMappingSource GetXmlMappingSource<T>() where T : class, IDataEntity
        {
            Type type = typeof(T);
            XmlMappingSource mappingSource = null;
            if (!dicTypeMappingSourceCollection.TryGetValue(type, out mappingSource))
            {
                lock (lockObj)
                {
                    if (!dicTypeMappingSourceCollection.TryGetValue(type, out mappingSource))
                    {
                        LinqDataMappingConfig config = CYTS.Aoyou.Framework.Config.ConfigFactory.GetConfig<LinqDataMappingConfig>();
                        string prefix = type.Name;
                        string path = System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "\\" + config.MappingFileLocation);
                        mappingSource = XmlMappingSource.FromUrl(path + prefix + ".mapping.xml");
                        dicTypeMappingSourceCollection[type] = mappingSource;
                    }
                }
            }

            return mappingSource;
        }
    }
}
